package com.pubg.servlet;

import com.pubg.entity.User;
import com.pubg.service.UserService;
import com.pubg.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "UserServlet",urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String opr = request.getParameter("opr");
        if ("login".equals(opr)){
            doLogin(request,response,out);
        }else if ("register".equals(opr)){
            try {
                doRegister(request,response,out);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if("isExistPhone".equals(opr)){
            String loginName = request.getParameter("loginName");
            if (userService.findByName(loginName)){
                //手机号已存在
                out.print("noExist");


            }else{
                out.print("ok");
            }
        }
    }

    /**
     * 注册
     * @param request
     * @param response
     * @param out
     */
    private void doRegister(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws ParseException {
        String loginName = request.getParameter("telephone");
        String password = request.getParameter( "password");
        String email = request.getParameter("email");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Date date = sdf.parse(sdf.format(new Date()));
        User user = new User(
                0,loginName,password,0,null,0,null,loginName,date,email
        );
        if(userService.insert(user)){
            out.println("<script type='text/javascript'>alert('注册成功！');location.href='../index.jsp';</script>");
        }else{
            out.println("<script type='text/javascript'>alert('注册失败，请填写正确的信息');location.href='logn.html';</script>");
        }

    }

    /**
     * 登陆
     * @param request
     * @param response
     * @param
     */
    private void doLogin(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws IOException, ServletException {
        String uname = request.getParameter("loginName");
        String upwd = request.getParameter("password");
        User user = userService.login(uname,upwd);
        if (user != null){
            request.getSession().setAttribute("user_admin",user);
            System.out.println(user.getLoginName());
            out.println("<script type='text/javascript'>alert('登陆成功');</script>");
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }else {
            out.println("<script type='text/javascript'>alert('用户名或密码错误，请重新登陆！');location.href='login.html';</script>");
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
